package com.wxp.demo09.system;

import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.wxp.demo09.R;
import com.wxp.demo09.system.receiver.AlarmReceiver;

/**
 * 2.3 系统广播-定时管理器AlarmManager
 */
public class AlarmActivity extends AppCompatActivity implements View.OnClickListener {

    private AlarmReceiver alarmReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_alarm);
        this.setTitle("2.3 系统广播-定时管理器AlarmManager");

        findViewById(R.id.btn_alarm).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        alarmReceiver.sendAlarm();
    }

    @Override
    protected void onStart() {
        super.onStart();
        //this.getApplicationContext()：避免当前页面关闭，引起内存泄露。
        alarmReceiver = new AlarmReceiver(this.getApplicationContext());
        //定义闹钟过滤器
        IntentFilter filter = new IntentFilter(AlarmReceiver.ALARM_ACTION);
        this.registerReceiver(alarmReceiver, filter, Context.RECEIVER_EXPORTED);
    }

    @Override
    protected void onStop() {
        super.onStop();
        this.unregisterReceiver(alarmReceiver);
    }

}